/*---------------------------------------------------
-----------------------------------------------------
Filename:	external_handles.m

Type:		maki
Author:		Martin Poehlmann aka Deimos
E-Mail:		martin@skinconsortium.com
Internet:	www.skinconsortium.com
		www.martin.deimos.de.vu
-----------------------------------------------------
Depending Files:
		scripts/suicore.maki
-----------------------------------------------------
---------------------------------------------------*/

#ifndef included
#error This script can only be compiled as a #include
#endif

#ifdef DEBUG
/** has only debug aims :) */

System.onLookForComponent(String guid) {
debugString(DEBUG_PREFIX "System.onLookForComponent ( "+ guid + " )", D_WTF);
}

#endif

/** the component switching */

System.onGetCancelComponent(String guid, boolean goingvisible) {
	debugString(DEBUG_PREFIX "System.onGetCancelComponent ( "+ guid + " , " + integerToString(goingvisible) + " )  {", D_WTF);
	debugString(DEBUG_PREFIX "   [Last Content: " + getPrivateString("Winamp SUI", "Component", "Media Library") + " ]", D_WTF);

	Boolean isShade = (player.getCurLayout() != normal);

	// Do Playlist Stuff First (is unlinked from SUI)

	if (guid == PL_GUID)
	{
		if (goingvisible && isShade)
		{
			debugString(DEBUG_PREFIX "   --> opening pl --- return from shade!", D_WTF);
			debugString(DEBUG_PREFIX "   return TRUE;", D_WTF);	
			debugString(DEBUG_PREFIX "}", D_WTF);
			switchToNoComp();
			player.switchToLayout("normal");
			normal.sendAction("load_comp", "pledit", 0,0,0,0);
			return TRUE;
		}
	}

	if (bypasscancel)
	{
		debugString(DEBUG_PREFIX "   --> bypasscancel", D_WTF);
		debugString(DEBUG_PREFIX "   return FALSE;", D_WTF);	
		debugString(DEBUG_PREFIX "}", D_WTF);

		return FALSE;
	}
	if (guid == ML_GUID && !goingvisible && hiding_ml)
	{
		debugString(DEBUG_PREFIX "   --> hiding_ml", D_WTF);
		debugString(DEBUG_PREFIX "   return FALSE;", D_WTF);	
		debugString(DEBUG_PREFIX "}", D_WTF);

		return FALSE;
	}
	if (guid == VIDEO_GUID && !goingvisible && hiding_video)
	{
		debugString(DEBUG_PREFIX "   --> hiding_video", D_WTF);
		debugString(DEBUG_PREFIX "   return FALSE;", D_WTF);	
		debugString(DEBUG_PREFIX "}", D_WTF);

		return FALSE;
	}
	if (guid == VIS_GUID && !goingvisible && hiding_vis)
	{
		debugString(DEBUG_PREFIX "   --> hiding_vis", D_WTF);
		debugString(DEBUG_PREFIX "   return FALSE;", D_WTF);	
		debugString(DEBUG_PREFIX "}", D_WTF);

		return FALSE;
	}
	if (guid == ML_GUID && goingvisible && showing_ml)
	{
		debugString(DEBUG_PREFIX "   --> showing_ml", D_WTF);
		debugString(DEBUG_PREFIX "   return FALSE;", D_WTF);	
		debugString(DEBUG_PREFIX "}", D_WTF);

		return FALSE;
	}
	if (guid == VIDEO_GUID && goingvisible && showing_video)
	{
		debugString(DEBUG_PREFIX "   --> showing_video", D_WTF);
		debugString(DEBUG_PREFIX "   return FALSE;", D_WTF);	
		debugString(DEBUG_PREFIX "}", D_WTF);

		return FALSE;
	}
	if (guid == VIS_GUID && goingvisible && showing_vis)
	{
		debugString(DEBUG_PREFIX "   --> showing_vis", D_WTF);
		debugString(DEBUG_PREFIX "   return FALSE;", D_WTF);	
		debugString(DEBUG_PREFIX "}", D_WTF);

		return FALSE;
	}


/*	if (player.getCurLayout() != normal)
	{
		player.switchToLayout("normal");
		setPrivateString("Winamp SUI", "Component", "Media Library");
		debugString(DEBUG_PREFIX "   --> normal layout isn't visible", D_WTF);
		debugString(DEBUG_PREFIX "   return FALSE;", D_WTF);	
		debugString(DEBUG_PREFIX "}", D_WTF);

		return TRUE;
	}*/

	String window_content =  getPrivateString("Winamp SUI", "Component", "Media Library");

	if (!goingvisible && window_content == "Hidden")
	{
		debugString(DEBUG_PREFIX "   --> SUI isn't visible", D_WTF);
		debugString(DEBUG_PREFIX "   return TRUE;", D_WTF);	
		debugString(DEBUG_PREFIX "}", D_WTF);
		return FALSE;
	}

	if (goingvisible) {
		if (guid == VIDEO_GUID && (window_content != "Video" || isShade)) {
			debugString(DEBUG_PREFIX "   --> external showing_video --- start Timer", D_WTF);
			debugString(DEBUG_PREFIX "   return FALSE;", D_WTF);	
			debugString(DEBUG_PREFIX "}", D_WTF);

			if (isShade)
			{
				sui_video.hide();
				player.switchToLayout("normal");
			}

			//setPrivateString("Winamp SUI", "Component", "Video");

			//--hideExp();
			hideBrw();
			hideVis();
			hideMl();
			//--hideCfg();

			if (window_content == "Hidden")
			{
				setPrivateString("Winamp SUI", "Hidden Component", "Video");
				dc_showSUI ();
				return TRUE;
			}

			dc_showVideo();

			return TRUE;
		} else if (guid == VIS_GUID && (window_content != "Vis" || isShade)) {

			if (vis_inbig_attrib.getData() == "0")
			{
				debugString(DEBUG_PREFIX "   --> vis shouldn't be shown in big sui!", D_WTF);
				debugString(DEBUG_PREFIX "   return FALSE;", D_WTF);	
				debugString(DEBUG_PREFIX "}", D_WTF);
				return FALSE;
			}

			debugString(DEBUG_PREFIX "   --> external showing_vis --- start Timer", D_WTF);
			debugString(DEBUG_PREFIX "   return TRUE;", D_WTF);	
			debugString(DEBUG_PREFIX "}", D_WTF);

			if (isShade)
			{
				sui_vis.hide();
				player.switchToLayout("normal");
			}

			//setPrivateString("Winamp SUI", "Component", "Vis");

			disablePSOVC();
			hideVideo();
			hideMl();
			//--hideExp();
			hideBrw();
			//--hideCfg();
			if (window_content == "Hidden")
			{
				setPrivateString("Winamp SUI", "Hidden Component", "Vis");
				dc_showSUI ();
				return TRUE;
			}
			dc_showVis();

			return TRUE;
		} else if (guid == ML_GUID && (window_content != "Media Library" || isShade)) {
			debugString(DEBUG_PREFIX "   --> external showing_ml --- start Timer", D_WTF);
			debugString(DEBUG_PREFIX "   return TRUE;", D_WTF);	
			debugString(DEBUG_PREFIX "}", D_WTF);

			if (isShade)
			{
				sui_ml.hide();
				player.switchToLayout("normal");
			}

			//setPrivateString("Winamp SUI", "Component", "Media Library");

			disablePSOVC();
			hideVideo();
			hideVis();
			//--hideExp();
			hideBrw();
			//--hideCfg();
			if (window_content == "Hidden")
			{
				setPrivateString("Winamp SUI", "Hidden Component", "Media Library");
				dc_showSUI ();
				return TRUE;
			}
			dc_showMl();

			return TRUE;
		}
	}
	if (!goingvisible) {
		if (guid == VIDEO_GUID && window_content == "Video") {
			debugString(DEBUG_PREFIX "   --> external hiding_video", D_WTF);
			if (getStatus() == 1 && isVideo())
			{
				debugString(DEBUG_PREFIX "   --> video is playing", D_WTF);
				debugString(DEBUG_PREFIX "   return TRUE;", D_WTF);
				debugString(DEBUG_PREFIX "}", D_WTF);
				return true;

			}
			else
			{
				disablePSOVC();
				hideVideo();
				hideVis();
				//--hideExp();
				hideBrw();
				//--hideCfg();
				dc_showMl(); // normal Component
				debugString(DEBUG_PREFIX "   --> no video is playing", D_WTF);
				debugString(DEBUG_PREFIX "   return FALSE;", D_WTF);
				debugString(DEBUG_PREFIX "}", D_WTF);
				return false;
			}
		}
		if (guid == VIS_GUID && window_content == "Vis") {
			if (vis_inbig_attrib.getData() == "0")
			{
				debugString(DEBUG_PREFIX "   --> vis shouldn't be closed in big sui!", D_WTF);
				debugString(DEBUG_PREFIX "   return FALSE;", D_WTF);	
				debugString(DEBUG_PREFIX "}", D_WTF);
				return FALSE;
			}
			if (getStatus() != STATUS_STOPPED && system.isVideo())
			{
				debugString(DEBUG_PREFIX "   --> external hiding_vis --- open Video", D_WTF);
				hideMl();
				hideVis();
				//--hideExp();
				hideBrw();
				//--hideCfg();
				dc_showVideo();
			} 
			else 
			{
				debugString(DEBUG_PREFIX "   --> external hiding_video --- hide Video", D_WTF);
				hideVideo();
				hideVis();
				//--hideExp();
				hideBrw();
				//--hideCfg();
				dc_showMl(); // normal Component
			}
			debugString(DEBUG_PREFIX "   return FALSE;", D_WTF);	
			debugString(DEBUG_PREFIX "}", D_WTF);
			return FALSE;
		}
		if (guid == ML_GUID && window_content == "Media Library") {
			if ((getStatus() == STATUS_PLAYING || getStatus() == STATUS_PAUSED) && isVideo())
			{
				debugString(DEBUG_PREFIX "   --> external hiding_ml --- open Video", D_WTF);
				debugString(DEBUG_PREFIX "   return FALSE;", D_WTF);	
				debugString(DEBUG_PREFIX "}", D_WTF);
				hideMl();
				hideVis();
				//--hideExp();
				hideBrw();
				//--hideCfg();
				dc_showVideo();
				return FALSE;
			} 
			else 
			{
				debugString(DEBUG_PREFIX "   --> external hiding_ml --- switch to browser", D_WTF);
				debugString(DEBUG_PREFIX "   return flase;", D_WTF);	
				debugString(DEBUG_PREFIX "}", D_WTF);
				hideMl();
				hideVis();
				hideBrw();
				//--hideCfg();
				hideVideo();
				//--hideExp();
				dc_closeSUI();
				return false;
			}

		}
	}

	debugString(DEBUG_PREFIX "   --> Went thru", D_WTF);
	debugString(DEBUG_PREFIX "   return FALSE;", D_WTF);
	debugString(DEBUG_PREFIX "}", D_WTF);

	return FALSE;
}


Player.onSwitchToLayout (Layout _layout)
{
	if (_layout.getId() != "normal")
	{
		mychange = 1;
		//--sui_explorer_attrib.setData("0");
		sui_browser_attrib.setData("0");
		//--sui_config_attrib.setData("0");
		mychange = 0;
	}
}

/** Windows that aren't registered with winamp will be handled here,
	the visibility state is linked to config attribs */
/*--
sui_explorer_attrib.onDataChanged ()
{
	if (mychange) return;
	mychange = 1;
	String window_content = getPrivateString("Winamp SUI", "Component", "Media Library");
	if (getData() == "1")
	{
		if (player.getCurLayout() != normal)
		{
			player.switchToLayout("normal");
			hideMl();
			hideVis();
			hideBrw();
			//--hideCfg();
			hideVideo();
			dc_showExp();
			return;
		}
		if (window_content == "Hidden" )
		{
			setPrivateString("Winamp SUI", "Hidden Component", "Explorer");
			switchFromNoComp ();
			return;
		}
		switchToExp();
	}
	else
	{
		if ((getStatus() == STATUS_PLAYING || getStatus() == STATUS_PAUSED) && isVideo())
		{
			hideExp();
			dc_showVideo();
		} 
		else
		{
			hideExp();
			dc_showml();			
		}
	}
	mychange = 0;
}
--*/
System.onOpenUrl(string url)
{
	if (player.getCurLayout() != normal)
	{
		setPrivateString("Winamp SUI", "UrlXgive", url);
		player.switchToLayout("normal");
		hideMl();
		hideVis();
		//--hideCfg();
		hideVideo();
		//--hideExp();
		dc_showBrw();
		return 1;
	}
	String window_content = getPrivateString("Winamp SUI", "Component", "Media Library");
	if (window_content == "Browser")
	{
		browser brw = sui_brw.findObject("webbrowser");
		if (brw != NULL)
		{
			brw.sendAction ("openurl", url, 0, 0, 0, 0);			
		}
	}
	else
	{
		setPrivateString("Winamp SUI", "UrlXgive", url);
		debugString("System.onOpenUrl( "+url+" ); --> opening Browser", D_WTF);
		if (window_content == "Hidden")
		{
			setPrivateString("Winamp SUI", "Hidden Component", "Browser");
			switchFromNoComp ();
			return 1;
		}
		switchToBrw();		
	}
	return 1;
}

sui_window.onAction (String action, String param, Int x, int y, int p1, int p2, GuiObject source)
{
	if (strlower(action) == "browser_search")
	{
		browser brw = sui_brw.findObject("webbrowser");
		if (player.getCurLayout() != normal)
		{
			//setPrivateString("Winamp SUI", "UrlXgive", url);
			player.switchToLayout("normal");
			hideMl();
			hideVis();
			//--hideCfg();
			hideVideo();
			//--hideExp();
			dc_showBrw();
			brw.sendAction ("search", param, 0, 0, 0, 0);	
			return 1;
		}
		String window_content = getPrivateString("Winamp SUI", "Component", "Media Library");
		if (window_content == "Browser")
		{
			if (brw != NULL)
			{
				brw.sendAction ("search", param, 0, 0, 0, 0);			
			}
		}
		else
		{
			//setPrivateString("Winamp SUI", "UrlXgive", url);
			debugString("System.onOpenUrl( "+url+" ); --> opening Browser", D_WTF);
			if (window_content == "Hidden")
			{
				setPrivateString("Winamp SUI", "Hidden Component", "Browser");
				switchFromNoComp ();
				return 1;
			}
			switchToBrw();
			brw.sendAction ("search", param, 0, 0, 0, 0);	
		}
		return 1;
	}
	if (strlower(action) == "browser_navigate")
	{
		browser brw = sui_brw.findObject("webbrowser");
		if (player.getCurLayout() != normal)
		{
			//setPrivateString("Winamp SUI", "UrlXgive", url);
			player.switchToLayout("normal");
			hideMl();
			hideVis();
			//--hideCfg();
			hideVideo();
			//--hideExp();
			dc_showBrw();
			brw.sendAction ("openurl", param, 0, 0, 0, 0);	
			return 1;
		}
		String window_content = getPrivateString("Winamp SUI", "Component", "Media Library");
		if (window_content == "Browser")
		{
			if (brw != NULL)
			{
				brw.sendAction ("openurl", param, 0, 0, 0, 0);			
			}
		}
		else
		{
			//setPrivateString("Winamp SUI", "UrlXgive", url);
			debugString("System.onOpenUrl( "+url+" ); --> opening Browser", D_WTF);
			if (window_content == "Hidden")
			{
				setPrivateString("Winamp SUI", "Hidden Component", "Browser");
				switchFromNoComp ();
				return 1;
			}
			switchToBrw();
			brw.sendAction ("openurl", param, 0, 0, 0, 0);	
		}
		return 1;
	}
}


sui_browser_attrib.onDataChanged ()
{
	if (mychange) return;
	mychange = 1;
	String window_content = getPrivateString("Winamp SUI", "Component", "Media Library");
	if (getData() == "1")
	{
		if (player.getCurLayout() != "normal")
		{
			player.switchToLayout("normal");
			hideMl();
			hideVis();
			//--hideCfg();
			hideVideo();
			//--hideExp();
			dc_showBrw();
			return;
		}
		if (window_content == "Hidden")
		{
			setPrivateString("Winamp SUI", "Hidden Component", "Browser");
			switchFromNoComp ();
			return;
		}
		switchToBrw();
	}
	else
	{
		if ((getStatus() == STATUS_PLAYING || getStatus() == STATUS_PAUSED) && isVideo())
		{
			hideBrw();
			dc_showVideo();
		} 
		else
		{
			hideBrw();
			dc_showml();			
		}
	}
	mychange = 0;
}

/*--sui_config_attrib.onDataChanged ()
{
	if (mychange) return;
	mychange = 1;
	String window_content = getPrivateString("Winamp SUI", "Component", "Media Library");
	if (getData() == "1")
	{
		if (player.getCurLayout() != "normal")
		{
			player.switchToLayout("normal");
			hideMl();
			hideVis();
			hideBrw();
			hideCfg();
			hideVideo();
			hideExp();
			dc_showCfg();
			return;
		}
		if (window_content == "Hidden")
		{
			setPrivateString("Winamp SUI", "Hidden Component", "Config");
			switchFromNoComp ();
			return;
		}
		switchToCfg();
	}
	else
	{
		if ((getStatus() == STATUS_PLAYING || getStatus() == STATUS_PAUSED) && isVideo())
		{
			hideCfg();
			dc_showVideo();
		} 
		else
		{
			hideCfg();
			dc_showml();			
		}
	}
	mychange = 0;
}--*/


/** Switching vis between big / small component area */

vis_inbig_attrib.onDataChanged ()
{
	if (mychange) return;
	String window_content2 =  getPrivateString("Winamp SUI", "Component", "Cover");
	String window_content =  getPrivateString("Winamp SUI", "Component2", "Media Library");
	int xg = getPrivateInt("Winamp SUI", "ComponentXgive", 0);
	if (getData() == "1" && (ic_vis.getData() == "1" || ic_vis_fileinfo.getData() == "1"  || xg))
	{
		if (xg) setPrivateInt("Winamp SUI", "ComponentXgive", 0);
		switchToVis();
	}
	else if (getData() == "0" && sui_Vis.isVisible())
	{
		setPrivateInt("Winamp SUI", "ComponentXgive", 1);
		hideVis();
		switchToMl();
	}
}

/** this one gets if we are resizing the vis object */
/** (not needed anymore) DELETE FOR FINAL RELEASE

sui_vis.onResize (int x, int y, int w, int h)
{
	if (y != 220 && y != 0 && y != 221)
	{
		if ((getStatus() == STATUS_PLAYING || getStatus() == STATUS_PAUSED) && isVideo())
		{
			debugString(DEBUG_PREFIX "   --> external hiding_vis --- open Video",0);
			hideMl();
			onHideVis();
			hideExp();
			hideBrw();
			hideCfg();
			dc_showVideo();
		} 
		else 
		{
			debugString(DEBUG_PREFIX "   --> external hiding_video --- hide Video", 0);
			hideVideo();
			onHideVis();
			hideExp();
			hideBrw();
			hideCfg();
			dc_showMl(); // normal Component
		}		
	}
}
*/